package top.wilbert.collegelink.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import top.wilbert.collegelink.common.utils.R;
import top.wilbert.collegelink.message.entity.ChatListEntity;
import top.wilbert.collegelink.message.entity.ChatMessageEntity;
import top.wilbert.collegelink.message.entity.ChatUserLinkEntity;
import top.wilbert.collegelink.message.service.ChatListService;
import top.wilbert.collegelink.message.service.ChatMessageService;
import top.wilbert.collegelink.message.service.ChatService;
import top.wilbert.collegelink.message.service.ChatUserLinkService;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author:Jiiangyu
 * @CreatTime:2023-02-27-19:38
 */
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatListService chatListService;

    @Autowired
    private ChatMessageService chatMessageService;

    @Autowired
    private ChatUserLinkService chatUserLinkService;


    @Override
    public Long selectAssociation(Long fromUserId, Long toUserId) {

        ChatUserLinkEntity link = chatUserLinkService.getOne(new QueryWrapper<ChatUserLinkEntity>().eq("from_user_id", fromUserId)
                .eq("to_user_id", toUserId));
        return link.getLinkId();
    }

    @Override
    public void isFirstChat(Long fromUserId, Long toUserId) {

        QueryWrapper<ChatUserLinkEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_user_id", fromUserId)
                .eq("to_user_id", toUserId);

        ChatUserLinkEntity link = chatUserLinkService.getOne(queryWrapper);

        if (link == null) {
//            判断是不是第一次聊天，如果是会在主表生成一条记录返回用户聊天关系表id
            ChatUserLinkEntity newLink = new ChatUserLinkEntity();
            newLink.setFromUserId(fromUserId);
            newLink.setToUserId(toUserId);
            long currentTimeMillis = System.currentTimeMillis();
            newLink.setCreateTime(new Date(currentTimeMillis));

            chatUserLinkService.save(newLink);

//            在聊天列表表分别插入两条记录
            ChatListEntity chatListFromEntity = new ChatListEntity();
            ChatListEntity chatListToEntity = new ChatListEntity();

            chatListFromEntity.setLinkId(newLink.getLinkId());
            chatListToEntity.setLinkId(newLink.getLinkId());

            chatListFromEntity.setFromUserId(newLink.getFromUserId());
            chatListToEntity.setToUserId(newLink.getFromUserId());

            chatListFromEntity.setToUserId(newLink.getToUserId());
            chatListToEntity.setFromUserId(newLink.getToUserId());

            chatListFromEntity.setFromWindow(true);
            chatListToEntity.setFromWindow(false);

            chatListFromEntity.setToWindow(false);
            chatListToEntity.setToWindow(true);

            List<ChatListEntity> list = new ArrayList<>();
            list.add(chatListFromEntity);
            list.add(chatListToEntity);
            
            chatListService.saveBatch(list);

        }

    }

    @Override
    public void saveMessage(ChatMessageEntity chatMessageEntity) {
        chatMessageService.save(chatMessageEntity);
        Long linkId = chatMessageEntity.getLinkId();
        Long fromUserId = chatMessageEntity.getFromUserId();
        ChatListEntity chatListEntity = new ChatListEntity();
        chatListEntity.setFromWindow(true);
        chatListService.update(chatListEntity, new UpdateWrapper<ChatListEntity>().eq("link_id", linkId)
                                                                                  .eq("from_user_id", fromUserId));
    }

    @Override
    public R getFromUserChatList(Long fromUserId) {
        return null;
    }

    @Override
    public R getRecentChatRecords(Long fromUserId, Long toUserId, int currentIndex) {
        return null;
    }

    @Override
    public void updateWindows(Long fromUserId, Long toUserId) {

    }

    @Override
    public R getUnreadTotalNumber(Long userId) {
        return null;
    }

    @Override
    public void resetWindows(Long userId) {

    }
}
